package com.cyt.work.antiepidemic.service.impl;

import cn.hutool.core.date.DateUtil;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.BaseConstant;
import com.cyt.base.util.DictCode;
import com.cyt.base.util.ErrorCode;
import com.cyt.base.util.StringUtils;
import com.cyt.sys.model.po.SysUser;
import com.cyt.sys.service.impl.BaseServiceImpl;
import com.cyt.work.antiepidemic.dao.AntiepidemicTemperatureDao;
import com.cyt.work.antiepidemic.model.po.AntiepidemicTemperature;
import com.cyt.work.antiepidemic.model.vo.AntiepidemicTemperatureVo;
import com.cyt.work.antiepidemic.service.AntiepidemicTemperatureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * e门通-社区上报信息Service层实现类
 *
 * @author rong yuan
 * @date 2020-2-12 2:32:50
 */
@Service("antiepidemicTemperatureService")
public class AntiepidemicTemperatureServiceImpl extends BaseServiceImpl<AntiepidemicTemperature> implements AntiepidemicTemperatureService {

    @Resource
    private AntiepidemicTemperatureDao antiepidemicTemperatureDao;

    @Autowired
    public void setBaseDao(AntiepidemicTemperatureDao antiepidemicTemperatureDao) {
        this.baseDao = antiepidemicTemperatureDao;
    }

    /**
     * 分页查询
     *
     * @param antiepidemicTemperatureVo 接受参数对象
     * @param pageNum                   分页数
     * @param pageSize                  每页显示条数
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:32:50
     */
    @Override
    public ReturnResult page(AntiepidemicTemperatureVo antiepidemicTemperatureVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        PageHelper.orderBy(" create_time desc");
        List<AntiepidemicTemperatureVo> list = antiepidemicTemperatureDao.selectByCondition(antiepidemicTemperatureVo);
        PageInfo<AntiepidemicTemperatureVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 根据ID获取数据
     *
     * @param id 主键ID
     * @return AntiepidemicTemperatureVo
     * @author rong yuan
     * @date 2020-2-12 2:32:50
     */
    @Override
    public AntiepidemicTemperatureVo selectById(String id) {
        AntiepidemicTemperatureVo antiepidemicTemperatureVo = new AntiepidemicTemperatureVo();
        antiepidemicTemperatureVo.setId(id);
        List<AntiepidemicTemperatureVo> antiepidemicTemperatureVoList = antiepidemicTemperatureDao.selectByCondition(antiepidemicTemperatureVo);
        if (antiepidemicTemperatureVoList != null && antiepidemicTemperatureVoList.size() == 1) {
            antiepidemicTemperatureVo = antiepidemicTemperatureVoList.get(0);
        }
        return antiepidemicTemperatureVo;
    }

    /**
     * 添加或更新
     *
     * @param antiepidemicTemperatureVo 参数接受对象
     * @param sysUser                   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:32:50
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult addOrUpdate(AntiepidemicTemperatureVo antiepidemicTemperatureVo, SysUser sysUser) {
        if (StringUtils.isBlank(antiepidemicTemperatureVo.getId())) {
            AntiepidemicTemperature antiepidemicTemperature = new AntiepidemicTemperature(sysUser);
            antiepidemicTemperature.setApplyId(antiepidemicTemperatureVo.getApplyId());
            antiepidemicTemperature.setPersonId(antiepidemicTemperatureVo.getPersonId());
            antiepidemicTemperature.setRecordTime(DateUtil.parseDateTime(antiepidemicTemperatureVo.getRecordTime()));
            antiepidemicTemperature.setTemperatureStatus(antiepidemicTemperatureVo.getTemperatureStatus());
            antiepidemicTemperature.setTemperature(antiepidemicTemperatureVo.getTemperature());
            antiepidemicTemperatureDao.insert(antiepidemicTemperature);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_INSERT);
        } else {
            AntiepidemicTemperature antiepidemicTemperature = antiepidemicTemperatureDao.selectByPrimaryKey(antiepidemicTemperatureVo.getId());
            antiepidemicTemperature.setApplyId(antiepidemicTemperatureVo.getApplyId());
            antiepidemicTemperature.setPersonId(antiepidemicTemperatureVo.getPersonId());
            antiepidemicTemperature.setRecordTime(DateUtil.parseDateTime(antiepidemicTemperatureVo.getRecordTime()));
            antiepidemicTemperature.setTemperatureStatus(antiepidemicTemperatureVo.getTemperatureStatus());
            antiepidemicTemperature.setTemperature(antiepidemicTemperatureVo.getTemperature());
            antiepidemicTemperature.setUpdateTime(new Date());
            antiepidemicTemperature.setUpdateUser(sysUser.getId());
            antiepidemicTemperatureDao.updateByPrimaryKey(antiepidemicTemperature);
            return new ReturnResult(BaseConstant.DATABASE_SUCCEED_UPDATE);
        }
    }

    /**
     * 根据条件逻辑删除
     *
     * @param antiepidemicTemperatureVo 参数接受对象
     * @param sysUser                   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:32:50
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult remove(AntiepidemicTemperatureVo antiepidemicTemperatureVo, SysUser sysUser) {
        //要修改的字段
        AntiepidemicTemperature antiepidemicTemperature = new AntiepidemicTemperature();
        antiepidemicTemperature.setRemoveTag(DictCode.BASE_REMOVE_TAG_DELETE);
        antiepidemicTemperature.setUpdateUser(sysUser.getId());
        antiepidemicTemperature.setUpdateTime(new Date());
        //筛选条件
        Example example = new Example(AntiepidemicTemperature.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicTemperatureVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicTemperatureVo.getId().split(",")));
        }
        criteria.andEqualTo("removeTag", DictCode.BASE_REMOVE_TAG_NORMAL);
        antiepidemicTemperatureDao.updateByExampleSelective(antiepidemicTemperature, example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }

    /**
     * 根据条件物理删除
     *
     * @param antiepidemicTemperatureVo 参数接受对象
     * @param sysUser                   当前用户
     * @return ReturnResult
     * @author rong yuan
     * @date 2020-2-12 2:32:50
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnResult delete(AntiepidemicTemperatureVo antiepidemicTemperatureVo, SysUser sysUser) {
        //筛选条件
        Example example = new Example(AntiepidemicTemperature.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtils.isNotBlank(antiepidemicTemperatureVo.getId())) {
            criteria.andIn("id", Arrays.asList(antiepidemicTemperatureVo.getId().split(",")));
        }
        antiepidemicTemperatureDao.deleteByExample(example);
        return new ReturnResult(ErrorCode.CODE_0, BaseConstant.DATABASE_SUCCEED_DEL);
    }
}